package com.mpsc.multi.source.config.datasource;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

/**
 * 多数据源配置类
 */
//@Configuration
public class DataSourceConfig1 {
    //数据源1
  //  @Bean(name = "db1")
    //@ConfigurationProperties(prefix = "multi.datasource.db1") // application.properteis中对应属性的前缀
    public DataSource dataSource1() {
        HikariConfig hikariConfig=new HikariConfig();
        hikariConfig.setJdbcUrl("jdbc:mysql://localhost:3306/sharding_2?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC");
        hikariConfig.setUsername("root");
        hikariConfig.setPassword("123456");
        hikariConfig.setDriverClassName("com.mysql.jdbc.Driver");
        HikariDataSource hikariDataSource=new HikariDataSource(hikariConfig);
        return hikariDataSource;
        //return DataSourceBuilder.create().build();
    }
//
    //数据源2
   //@Bean(name = "db2")
    //@ConfigurationProperties(prefix = "spring.datasource.db2") // application.properteis中对应属性的前缀
    public DataSource dataSource2() {
        HikariConfig hikariConfig=new HikariConfig();
        hikariConfig.setJdbcUrl("jdbc:mysql://localhost:3306/sharding_1?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC");
        hikariConfig.setUsername("root");
        hikariConfig.setPassword("123456");
        hikariConfig.setDriverClassName("com.mysql.jdbc.Driver");
        HikariDataSource hikariDataSource=new HikariDataSource(hikariConfig);
        return hikariDataSource;
        //return DataSourceBuilder.create().build();
    }

    /**
     * 动态数据源
     *
     * @return
     */
//    @Primary
//    @Bean(name = "dynamicDataSource")
    public DataSource dynamicDataSource() {
        DynamicDataSource dynamicDataSource = new DynamicDataSource();
        // 默认数据源
        dynamicDataSource.setDefaultTargetDataSource(dataSource1());
        // 配置多数据源
        Map<Object, Object> dsMap = new HashMap();
        dsMap.put("db1", dataSource1());
        dsMap.put("db2", dataSource2());
        dynamicDataSource.setTargetDataSources(dsMap);
        return dynamicDataSource;
    }

//    @Bean("sqlSessionFactory")
//    public SqlSessionFactory sqlSessionFactory() throws Exception {
//        SqlSessionFactoryBean sqlSessionFactory = new SqlSessionFactoryBean();
//        sqlSessionFactory.setDataSource(dynamicDataSource());
//        // 使用自定义的多数据源事务工厂，如采用JdbcTemplate方式可不配置
//        sqlSessionFactory.setTransactionFactory(new MultiDataSourceTransactionFactory());
//        //添加XML目录
//        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
//        //sqlSessionFactory.setConfigLocation(resolver.getResource("classpath:mybatis-config.xml"));
//        sqlSessionFactory.setMapperLocations(resolver.getResources("classpath*:com/mpsc/multi/source/mapper/*.xml"));
//        return sqlSessionFactory.getObject();
//    }

//    @Bean
//    public DataSource multipleDataSource(@Qualifier("datasource1") DataSource dataSource1,
//                                         @Qualifier("datasource2") DataSource dataSource2) {
//        DynamicDataSource dynamicDataSource = new DynamicDataSource();
//        Map<Object, Object> targetDataSources = new HashMap<>();
//        targetDataSources.put("dataSource1", dataSource1);
//        targetDataSources.put("dataSource2", dataSource2);
//        dynamicDataSource.setTargetDataSources(targetDataSources);
//        dynamicDataSource.setDefaultTargetDataSource(dataSource1);
//        return dynamicDataSource;
//    }


    /**
     * 配置@Transactional注解事物
     *
     * @return
     */
    @Bean
    public PlatformTransactionManager transactionManager() {
        return new DataSourceTransactionManager(dynamicDataSource());
    }
}
